/** Audit frontend framework release build script */
var pkg = require('./package.json');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var header = require('gulp-header');
var del = require('del');
var minimist = require('minimist');
var browserify = require('gulp-browserify');

var inds = pkg.independents;

//获取参数
var argv = require('minimist')(process.argv.slice(2), {
        default: {
            ver: 'all'
        }
    }),

    notes = [
        '/** <%= pkg.name %>-v<%= pkg.version %> <%= pkg.license %> License By <%= pkg.homepage %> */\n <%= js %>', {
            'pkg': pkg,
            'js': ';'
        }
    ],

    release = './dist/v-' + pkg.version,

    //任务
    tasks = {
        //处理浏览器端脚本间的依赖
        browserify: function () {
            return gulp.src('source/audit.js')
                .pipe(browserify({
                    insertGlobals: true,
                    debug: true
                }))
                .pipe(rename('audit.debug.js'))
                .pipe(gulp.dest(release));
        },
        //打包所有模块并压缩js模块
        minjs: function (ver) {
            //可指定模块压缩，eg：gulp minjs
            var src = [
                release + '/*.js'
            ];
            return gulp.src(src).pipe(uglify())
                .pipe(header.apply(null, notes))
                .pipe(rename('audit.min.js'))
                .pipe(gulp.dest(release));

        },

        //打包所有模块但不压缩
        alljs: function (ver) {
            var src = [
                release + '/*.js'
            ];
            return gulp.src(src)
                .pipe(header.apply(null, notes))
                .pipe(gulp.dest(release));
        },

        //复制iconfont文件
        font: function (ver) {
            return gulp.src('source/font/*')
                .pipe(rename({}))
                .pipe(gulp.dest(release + '/font'));
        },

        //复制组件可能所需的非css和js资源
        mv: function (ver) {
            var src = ['source/**/*.{png,jpg,gif,html,mp3,json}']

            return gulp.src(src)
                .pipe(rename({}))
                .pipe(gulp.dest(release));
        },

        clean: function () {
            return del(['./dist/*']);
        }
    };

gulp.task('clean', tasks.clean);
gulp.task('browserify', tasks.browserify);
gulp.task('minjs', tasks.minjs);
gulp.task('alljs', tasks.alljs);
gulp.task('font', tasks.font);
gulp.task('mv', tasks.mv);

//打包发行版
gulp.task('default', gulp.series('clean', 'browserify', 'minjs', async function finished() {
    console.log('Audit Frontend Framework build success\n************************');
}));